package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.system.domain.SySelfServiceBookingOrder;
import com.ruoyi.system.dto.SelfServiceOrderQueryDTO;
import com.ruoyi.system.enumeration.SelfServiceBookingStatusEnum;
import com.ruoyi.system.mapper.SySelfServiceBookingOrderMapper;
import com.ruoyi.system.service.ISelfServiceBookingOrderService;
import com.ruoyi.system.util.StoreIdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SelfServiceBookingOrderServiceImpl extends ServiceImpl<SySelfServiceBookingOrderMapper, SySelfServiceBookingOrder> implements ISelfServiceBookingOrderService {

    @Autowired
    private StoreIdUtils storeIdUtils;
    @Override
    public IPage<SySelfServiceBookingOrder> list(SelfServiceOrderQueryDTO queryDTO) {
        if (queryDTO == null) {
            throw new RuntimeException("参数错误,请求对象不能为空");
        }
        LambdaQueryWrapper<SySelfServiceBookingOrder> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(StringUtils.isNotEmpty(queryDTO.getVipMessage()), SySelfServiceBookingOrder::getVipMessage, queryDTO.getVipMessage());
        queryWrapper.eq(StringUtils.isNotEmpty(queryDTO.getBookingNumber()), SySelfServiceBookingOrder::getBookingNumber, queryDTO.getBookingNumber());
        queryWrapper.eq(queryDTO.getStatus() != null, SySelfServiceBookingOrder::getStatus, queryDTO.getStatus());
        queryWrapper.eq(queryDTO.getOrderType() != null, SySelfServiceBookingOrder::getOrderType, queryDTO.getOrderType());
        queryWrapper.eq(StringUtils.isNotEmpty(queryDTO.getDesk()), SySelfServiceBookingOrder::getDesk, queryDTO.getDesk());
        queryWrapper.eq(StringUtils.isNotEmpty(queryDTO.getTableClassification()), SySelfServiceBookingOrder::getTableClassification, queryDTO.getTableClassification());
        queryWrapper.eq(SySelfServiceBookingOrder::getStoreId,storeIdUtils.getStoreId());
        if (queryDTO.getBookingStartTime() != null && queryDTO.getBookingEndTime() != null) {
            queryWrapper.between(SySelfServiceBookingOrder::getSstimatedArrivalTime, queryDTO.getBookingStartTime(), queryDTO.getBookingEndTime());
        }
        if (queryDTO.getOrderStartTime() != null && queryDTO.getOrderEndTime() != null) {
            queryWrapper.between(SySelfServiceBookingOrder::getOrderTime, queryDTO.getOrderStartTime(), queryDTO.getOrderEndTime());
        }
//        List<SySelfServiceBookingOrder> list = this.list(queryWrapper);
        IPage<SySelfServiceBookingOrder> paged = this.page(new Page<>(queryDTO.getPageNum(), queryDTO.getPageSize()), queryWrapper);
        if (paged.getRecords() != null) {
            paged.getRecords().forEach(item -> {
                Integer status = item.getStatus();
                if (status!=null){
                    item.setStatusDesc(SelfServiceBookingStatusEnum.getDescriptionByCode(status));
                }
            });
        }
        return paged;
    }
}
